package com.yanqu.road.dao.impl.activity.summerquiz;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.summerquiz.CrossSummerQuizUserData;
import com.yanqu.road.entity.activity.summerquiz.SummerQuizDetail;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossSummerQuizUserDataDaoImpl extends TempDataObjectDao<CrossSummerQuizUserData> {

    public static String getCrossSummerQuizUserDataSql = "select * from `t_c_summer_quiz_user_data` where `activityId`=?";
    public Map<Integer, Map<Long, CrossSummerQuizUserData>> getCrossSummerQuizUserData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Long, CrossSummerQuizUserData>> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossSummerQuizUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossSummerQuizUserData data = parse(rs);
                if(!map.containsKey(data.getGameActivityId())){
                    map.put(data.getGameActivityId(), new ConcurrentHashMap<>());
                }
                map.get(data.getGameActivityId()).put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateCrossSummerQuizUserDataSql = "update `t_c_summer_quiz_user_data` set gameActivityId=?,serverId=?,quizDetailMap=?,guessScore=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(CrossSummerQuizUserData crossSummerQuizUserData) {
        crossSummerQuizUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossSummerQuizUserData.getGameActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSummerQuizUserData.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSummerQuizUserData.getQuizDetailMap())));
        para.put(i++, new DbParameter(Types.BIGINT, crossSummerQuizUserData.getGuessScore()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSummerQuizUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSummerQuizUserData.getUserId()));
        if (getDb().executeNoneQuery(updateCrossSummerQuizUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossSummerQuizUserDataSql = "insert into `t_c_summer_quiz_user_data`(activityId,userId,gameActivityId,serverId,quizDetailMap,guessScore) values (?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossSummerQuizUserData crossSummerQuizUserData) {
        crossSummerQuizUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossSummerQuizUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSummerQuizUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSummerQuizUserData.getGameActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSummerQuizUserData.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSummerQuizUserData.getQuizDetailMap())));
        para.put(i++, new DbParameter(Types.BIGINT, crossSummerQuizUserData.getGuessScore()));
        if (getDb().executeNoneQuery(insertCrossSummerQuizUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossSummerQuizUserData parseData(ResultSet rs) throws SQLException {
        CrossSummerQuizUserData crossSummerQuizUserData = new CrossSummerQuizUserData();
        crossSummerQuizUserData.setActivityId(rs.getInt("activityId"));
        crossSummerQuizUserData.setUserId(rs.getLong("userId"));
        crossSummerQuizUserData.setGameActivityId(rs.getInt("gameActivityId"));
        crossSummerQuizUserData.setServerId(rs.getLong("serverId"));
        crossSummerQuizUserData.setQuizDetailMap(JSON.parseObject(rs.getString("quizDetailMap"), new TypeReference<Map<Integer, SummerQuizDetail>>() {}));
        crossSummerQuizUserData.setGuessScore(rs.getLong("guessScore"));

        return crossSummerQuizUserData;
    }
}
